package com.jycrm.dao.sms.impl;

import java.sql.SQLException;
import java.util.List;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Query;
import net.sf.hibernate.Session;

import org.springframework.orm.hibernate.HibernateCallback;
import org.springframework.orm.ibatis.SqlMapClientCallback;

import com.ibatis.sqlmap.client.SqlMapExecutor;
import com.jycrm.dao.core.AbstractService;
import com.jycrm.dao.sms.ISmsSendBufferDao;
import com.jycrm.domain.sms.SmsSendBuffer;

public class SmsSendBufferDaoImpl extends AbstractService implements ISmsSendBufferDao{

	public void save(SmsSendBuffer sms) {
		this.persistenceManager.create(sms);
	}

	public void update(SmsSendBuffer sms) {
		this.persistenceManager.update(sms);
		
	}

	public void delete(SmsSendBuffer sms) {
		this.persistenceManager.delete(sms);
		
	}

	public SmsSendBuffer SmsSendBufferByID(String id) {
		return (SmsSendBuffer) this.queryManager.getObjectById(SmsSendBuffer.class,id);
		
	}
	
	public List getSmsSendBufferPageList(final int pageNo, final int pageSzie ){
		HibernateCallback callback =new HibernateCallback(){
			public Object doInHibernate(Session session) throws HibernateException {
		          Query query =  session.createQuery("FROM SmsSendBuffer buffer order by buffer.priority");
		          query.setFirstResult(pageNo);
		          query.setMaxResults(pageSzie);		          
		          return query.list();
		        }	
		};
		
		return this.queryManager.findPagination(callback);
	}
	
	public Long getSmsStatusSeq(){
		Long l =(Long) this.sqlMapManager.getSqlMapClientTemplate().queryForObject("getSmsStatusSeq",null); 
		return  l;
	}
	
	public void  BatchDeleteSmsSendBuffer(final SmsSendBuffer[] sms){
		this.sqlMapManager.getSqlMapClientTemplate().execute(new SqlMapClientCallback(){

			public Object doInSqlMapClient(SqlMapExecutor sqlMapExecutor) throws SQLException {
				
				if(sms!=null && sms.length>0){
					//sqlMapExecutor.startBatch();
					for(int i=0; i< sms.length; i++){
						sqlMapExecutor.delete("deleteSmsSendBuffer",sms[i].getId());
					}
					//sqlMapExecutor.executeBatch();
					
					
				}
				return new Boolean(true);
			}
			 
			
			
		});
	}
	
	public void  deleteSmsSendBuffer(Long id ){
		this.sqlMapManager.getSqlMapClientTemplate().delete("deleteSmsSendBuffer",id);
	}

}
